
#ifndef _SOCKET_SERVICE_H_
#define _SOCKET_SERVICE_H_


#ifdef _MSC_VER
	#include <winsock2.h>
	#include <time.h>
#else
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <sys/socketvar.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>

	#include <netdb.h>
	#include <unistd.h>

	#include <sys/select.h>			/* select(,,,)*/
	#include <sys/signal.h> 		/* signal(..)*/
	#include <sys/time.h>			/* time(),ctime(),wait3() */
	#include <sys/resource.h>		/* wait3() */
	#include <sys/wait.h>			/* wait3() */
	#include <sys/errno.h>			/* extern int errno */
#endif

#include <string.h>
#include <string>

#define R_SUCCESS         0
#define R_FAILURE        -1

#define PROTOCOL_TCP      1
#define PROTOCOL_UDP      2

/**
 * @class SocketService
 * 
 * @brief create TCP/UDP server/client and send/recv message
 *
 * @version V1.0 2010-10-15
 *
 * @author SHL
 *
 *
 */
class SocketService{
public:
	/// Constructor
	SocketService();

	/// Destructor
	~SocketService();
	
	/// Create TCP/UDP server
	/**
	* @param[in] protocol - PROTOCOL_TCP / PROTOCOL_UDP
	* @param[in] port - port number
	* @param[in] queueLen - max request queue for TCP, ignored for UDP 
	*/
	int createServer(int protocol, int port, int queueLen=5);

	/// Create TCP/UDP client
	/**
	* @param[in] protocol - PROTOCOL_TCP / PROTOCOL_UDP
	* @param[in] server - IP address or domain name of server which client will access to
	* @param[in] port - port number which client will access to
	*/
	int createClient(int protocol, char *server, int port);

	/// Accept tcp connection from client
	/**
	* @param[in] serverSocket - socket of server
	* @param[in/out] clientIP - IP address of client
	* @return socket of client
	*/
	int acceptTCP(int serverSocket, char *clientIP);

	/// Close socket
	void closeSocket(int sock);

	/// Send message
	/**
	* @param[in] sock - socket
	* @param[in] buff - buffer to send
	* @param[in] buffLen - length of buffer to send
	* @param[in] timeout - timeout for sending message
	*/
	int sendMsg(int sock, char *buff, int buffLen, int timeout=30);

	/// Receive message
	/**
	* @param[in] sock - socket
	* @param[in/out] buff - buffer to save received message
	* @param[in] buffLen - length of buffer
	* @param[in] timeout - timeout for receiving message
	*/
	int recvMsg(int sock, char *buff, int buffLen, int timeout=30);

private:
	
};

#endif
